<?php
/**
 */

class user extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

	// 添加用户
	public function add($arr = array())
	{
		return $this->db->insert('user',$arr);
	}

	// 登录
	public function login($arr = array())
	{
		/*$this->db->select('username,pwd,authority');
		$res = $this->db->get_where('user',$arr);
		return $res->row_array();*/

		$sql = "select userId,username,pwd,authority from user WHERE username ='{$arr['username']}'";
		$res = $this->db->query($sql)->row_array();
		return $res;
	}

	// 查询所有用户
	public function listUser($offset, $limit,$name)
	{
		if($name)
			$this->db->where('username',$name);
		return $this->db->get('user',$limit, $offset)->result_array();
	}

	// 统计用户量
	public function count()
	{
		$sql = 'select count(*) as num from user';
		$res = $this->db->query($sql)->row_array();
		return $res['num'];
	}

	// 通过用户名查询用户
	public function someOne1($name)
	{
		return $this->db->get_where('user',array('username'=>$name))->row_array();
	}

	// 通过用户ID查询用户
	public function someOne2($id)
	{
		return $this->db->get_where('user',array('userId'=>$id))->row_array();
	}

	// 通过用户名更新用户信息
	public function up($arr,$name)
	{
		$this->db->where('username',$name);
		return $this->db->update('user',$arr);
	}

	// 删除用户
	public function del($id)
	{
		return $this->db->delete('user',array('userId'=>$id));
	}

	// 通过用户ID修改用户信息
	public function upById($arr,$id)
	{
		return $this->db->where('userId', $id)->update('user', $arr);
	}

}